package cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.commodity;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.commodity.vo.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.commodity.CommodityDO;
import cn.iocoder.yudao.module.fengdong.smartelderly.convert.commodity.CommodityConvert;
import cn.iocoder.yudao.module.fengdong.smartelderly.service.commodity.CommodityService;

@Tag(name = "管理后台 - 商品")
@RestController
@RequestMapping("/fengdong.smartelderly/commodity")
@Validated
public class CommodityController {

    @Resource
    private CommodityService commodityService;

    @PostMapping("/create")
    @Operation(summary = "创建商品")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:commodity:create')")
    public CommonResult<Long> createCommodity(@Valid @RequestBody CommodityCreateReqVO createReqVO) {
        return success(commodityService.createCommodity(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:commodity:update')")
    public CommonResult<Boolean> updateCommodity(@Valid @RequestBody CommodityUpdateReqVO updateReqVO) {
        commodityService.updateCommodity(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:commodity:delete')")
    public CommonResult<Boolean> deleteCommodity(@RequestParam("id") Long id) {
        commodityService.deleteCommodity(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:commodity:query')")
    public CommonResult<CommodityRespVO> getCommodity(@RequestParam("id") Long id) {
        CommodityDO commodity = commodityService.getCommodity(id);
        return success(CommodityConvert.INSTANCE.convert(commodity));
    }

    @GetMapping("/list")
    @Operation(summary = "获得商品列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:commodity:query')")
    public CommonResult<List<CommodityRespVO>> getCommodityList(@RequestParam("ids") Collection<Long> ids) {
        List<CommodityDO> list = commodityService.getCommodityList(ids);
        return success(CommodityConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品分页")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:commodity:query')")
    public CommonResult<PageResult<CommodityRespVO>> getCommodityPage(@Valid CommodityPageReqVO pageVO) {
        PageResult<CommodityDO> pageResult = commodityService.getCommodityPage(pageVO);
        return success(CommodityConvert.INSTANCE.convertPage(pageResult));
    }

//    @GetMapping("/querypage")
//    @Operation(summary = "获得商品分页")
//    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:commodity:querypage')")
//    public CommonResult<IPage<CommodityRespIpageVO>> getCommodityIPage(@Valid CommodityIPageReqVO pageVO) {
//        IPage<CommodityRespIpageVO> pageResult = commodityService.getCommodityIPage(pageVO);
//        return success(pageResult);
//    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商品 Excel")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:commodity:export')")
    @OperateLog(type = EXPORT)
    public void exportCommodityExcel(@Valid CommodityExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CommodityDO> list = commodityService.getCommodityList(exportReqVO);
        // 导出 Excel
        List<CommodityExcelVO> datas = CommodityConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "商品.xls", "数据", CommodityExcelVO.class, datas);
    }

}
